//
//  ClubIntegralStoreTopView.swift
//  77Club
//
//  Created by Apple on 2024/9/11.
//

import UIKit

class ClubIntegralStoreTopView: UIView {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_whiteBack").end()
    private let bgView = c_make_ex(UIView.self)
        .bgColor("#17181A").end()
    private let integralLabel = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold).end()
    private let infoButton = c_make_ex(UIButton.self)
        .text("兑换明细").color(.white.alpha(0.8))
        .font(14.font_semiBold).end()
    
    private var allButton = UIButton(type: .custom)
    private var virtualButton = UIButton(type: .custom)
    private var realButton = UIButton(type: .custom)
    
    var backBlock: (() -> Void)?
    var infoBlock: (() -> Void)?
    var updateCateBlock: ((_ type: Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            bgView
            backButton
            integralLabel
            infoButton
            allButton
            virtualButton
            realButton
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(0).height(110.0.Scale)
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        integralLabel.leading(64.0.Scale).trailing(100.0.Scale)
        integralLabel.CenterY == backButton.CenterY
        infoButton.trailing(12).width(80.0.Scale).height(44.0.Scale)
        infoButton.CenterY == backButton.CenterY
        allButton.leading(12.0.Scale).width(50.0.Scale).height(24.0.Scale)
        allButton.Top == bgView.Bottom + 12.0.Scale
        virtualButton.width(50.0.Scale).height(24.0.Scale)
        virtualButton.Top == bgView.Bottom + 12.0.Scale
        virtualButton.Leading == allButton.Trailing + 4.0.Scale
        realButton.width(50.0.Scale).height(24.0.Scale)
        realButton.Top == bgView.Bottom + 12.0.Scale
        realButton.Leading == virtualButton.Trailing + 4.0.Scale
        
        setupCateButton(button: allButton, title: "全部", tag: 100)
        setupCateButton(button: virtualButton, title: "虚拟", tag: 101)
        setupCateButton(button: realButton, title: "实物", tag: 102)
        
        allButton.isSelected = true
        virtualButton.isSelected = false
        realButton.isSelected = false
        integralLabel.text = "我的积分：" + ClubUser.point
        
        allButton.layer.cornerRadius = 12.0
        allButton.layer.masksToBounds = true
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        infoButton.addTarget(self, action: #selector(clickInfoButton), for: .touchUpInside)
    }
    
    @objc private func clickBackButton(){
        backBlock?()
    }
    
    @objc private func clickInfoButton(){
        infoBlock?()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        bgView.rae.corner(12.0, corners: [.layerMinXMaxYCorner,.layerMaxXMaxYCorner])
    }
    
    private func setupCateButton(button: UIButton,title: String, tag: Int) {
        
        button.setTitle(title, for: .normal)
        button.setTitleColor("#2E3033".toColor, for: .selected)
        button.setTitleColor("#47494D".toColor, for: .normal)
        button.setBackgroundImage(UIImage.colorToImage(color: .clear), for: .normal)
        button.setBackgroundImage(UIImage.colorToImage(color: .white), for: .selected)
        button.titleLabel?.font = 12.font_semiBold
        button.addTarget(self, action: #selector(clickCateButton), for: .touchUpInside)
        button.tag = tag
    }
    
    @objc private func clickCateButton(button: UIButton){
        
        if button.tag == 100 && !button.isSelected {
            
            button.isSelected = true
            virtualButton.isSelected = false
            realButton.isSelected = false
            
            button.layer.cornerRadius = 12.0
            button.layer.masksToBounds = true
            virtualButton.layer.cornerRadius = 0
            virtualButton.layer.masksToBounds = true
            realButton.layer.cornerRadius = 0
            realButton.layer.masksToBounds = true
            
            updateCateBlock?(button.tag)
        }else if button.tag == 101 && !button.isSelected {
            
            button.isSelected = true
            allButton.isSelected = false
            realButton.isSelected = false
            
            button.layer.cornerRadius = 12.0
            button.layer.masksToBounds = true
            allButton.layer.cornerRadius = 0
            allButton.layer.masksToBounds = true
            realButton.layer.cornerRadius = 0
            realButton.layer.masksToBounds = true
            
            updateCateBlock?(button.tag)
        }else if button.tag == 102 && !button.isSelected {
            
            button.isSelected = true
            allButton.isSelected = false
            virtualButton.isSelected = false
            
            button.layer.cornerRadius = 12.0
            button.layer.masksToBounds = true
            allButton.layer.cornerRadius = 0
            allButton.layer.masksToBounds = true
            virtualButton.layer.cornerRadius = 0
            virtualButton.layer.masksToBounds = true
            
            updateCateBlock?(button.tag)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: 积分商城cell
class ClubMallGoodsCell: UICollectionViewCell {

    private let bgView = c_make_ex(UIView.self)
        .corner(12.0.Scale).bgColor(.white).end()
    private let goodsImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(13.font_semiBold).alignment(.center).end()
    private let integralLabel =  c_make_ex(UILabel.self)
        .color("#FF7500").font(13.font_semiBold).alignment(.center)
        .border("#FF7500".toColor.alpha(0.5), width: 1.5).corner(8.0.Scale).end()
    
    var model: ClubMallListModel? {
        didSet{
            guard let md = model else { return }
            
            goodsImageV.loadImage(url: md.goods_thumb)
            nameLabel.text = md.goods_name
            integralLabel.text = md.market_price + " 积分"
        }
    }
    
    var collectModel: ClubCollectGoodsModel?{
        didSet{
            guard let md = collectModel else { return }
            goodsImageV.loadImage(url: md.icon)
            nameLabel.text = md.name
            integralLabel.text = md.money + " 积分"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                goodsImageV
                nameLabel
                integralLabel
            }
        }
        
        bgView.fillContainer()
        goodsImageV.leading(0).trailing(0).top(0).height(170.0.Scale)
        nameLabel.leading(8).trailing(8)
        nameLabel.Top == goodsImageV.Bottom + 8.0.Scale
        integralLabel.leading(8).trailing(8).height(26.0.Scale)
        integralLabel.Top == nameLabel.Bottom + 8.0.Scale
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
